/*--------------------------------------------------------------------------------------*\
**
** adv_ferry.cpp
**
** Heroes IV
** Copyright 2000, The 3DO Company
**
\*--------------------------------------------------------------------------------------*/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "adventure_events_remove.h"

#include "adventure_map.h"
#include "adventure_object.h"
#include "adventure_object_ptr.h"
#include "compressed_filter.h"
#include "game_time.h"
#include "saved_game_header.h"

namespace 
{
	const int k_memory_event_buffer_size	= 0;
	const int k_event_lifespan				= 0*1000;			//in mili-seconds
};

t_adventure_event_remove::t_adventure_event_remove ( ) 
: t_adventure_event_base ( k_remove_event ) 
{ 	
	m_event_buffer			= new t_memory_buffer_counted ( k_memory_event_buffer_size ) ;
	m_event_state_buffer	= new t_memory_buffer_counted ( k_memory_event_buffer_size ) ;
	m_has_state_buffer		= false;
}

t_adventure_event_remove::t_adventure_event_remove ( t_adventure_object* adv_object, t_level_map_point_2d point ) 
: t_adventure_event_base ( k_remove_event ) 
{ 
	m_global_id			= adv_object->get_global_id();
	m_event_buffer		= t_adventure_event_functions::create_event_buffer(adv_object);
	m_event_point		= point;
	m_has_state_buffer	= false;
}

t_adventure_event_remove::t_adventure_event_remove ( t_adventure_object* adv_object, t_level_map_point_2d point, 
															t_memory_buffer_counted_ptr adv_buffer_pre_update) 
: t_adventure_event_base ( k_remove_event ) 
{ 
	m_global_id				= adv_object->get_global_id();
	m_event_buffer			= t_adventure_event_functions::create_event_buffer(adv_object);
	m_event_state_buffer	= adv_buffer_pre_update->clone();
	m_event_point			= point;
	m_has_state_buffer		= true;
}

void t_adventure_event_remove::execute_event( t_adventure_map* map, t_saved_game_header const & header ) 
{
	int size = m_event_buffer->size();
	
	map->remove_object(m_global_id);
	
	m_time_end = get_time() + k_event_lifespan;
	
	return;
}

void t_adventure_event_remove::undo_event( t_adventure_map* map , t_saved_game_header const & header )
{	
	t_adventure_event_functions::create_and_place_adv_object( map, m_event_buffer, header );
	
	if (m_event_state_buffer.get() != NULL && m_has_state_buffer == true)
	{	
		t_adventure_object * object = map->get_adventure_object_with_gid ( m_global_id );
		
		if (object == NULL) 
			return;

		t_adventure_event_functions::read_object_from_state_cache (object, m_event_state_buffer );
	}
}

bool t_adventure_event_remove::read( std::streambuf & buffer , int version )
{
	m_event_buffer->clear_buffer();

	int event_buffer_flag  = 	get<t_uint32> ( buffer );

	if (event_buffer_flag != 0)
	{
		m_event_buffer->read( buffer );
	}
	
	if (version >= 28)
	{
		int event_buffer_state_flag  = 	get<t_uint32> ( buffer );
		
		if (event_buffer_state_flag != 0)
		{
			m_event_state_buffer->read( buffer );
		}

		m_has_state_buffer = get<bool> ( buffer );
	}

	return t_adventure_event_base::read( buffer , version );
}

bool t_adventure_event_remove::write( std::streambuf & buffer )
{
	if (m_event_buffer.get() != 0)
	{
		put<t_uint32> (buffer, 1);
		
		m_event_buffer->write( buffer );
	}
	else
		put<t_uint32> (buffer, 0);
	
	if (m_event_state_buffer.get() != 0)
	{
		put<t_uint32> (buffer, 1);
		
		m_event_state_buffer->write( buffer );
	}
	else
		put<t_uint32> (buffer, 0);

	put<bool> (buffer, m_has_state_buffer);

	return t_adventure_event_base::write( buffer );
}
